package org.oylan.top.shiro;


import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.util.ByteSource;

/**
 * Created by A on 2019/2/3.
 */
public class UserRealm extends AuthenticatingRealm {

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("realm is gooing");
        //  token
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;

        String username = token.getUsername();

        String password = "1234567";

        // 认证信息
        SimpleAuthenticationInfo info = null;

        // 盐
//        ByteSource salt = ByteSource.Util.bytes(username);
        Object salt = null;
        // 加密
        SimpleHash simpleHash = new SimpleHash("MD5",password,salt,4);

        try {
            info =   new SimpleAuthenticationInfo(username,simpleHash,this.getName());
        }catch (UnknownAccountException e){
            throw new UnknownAccountException("用户不存在");
        }catch (DisabledAccountException e){
            throw new DisabledAccountException("用户身份有问题");
        }catch (IncorrectCredentialsException e){
            // 密码错误
            throw new DisabledAccountException("密码错误");
        }catch (AuthenticationException e){
            throw new DisabledAccountException("认证失败");
        }
        return info;
    }
}
